angular.module('smartOlwApp')
	.config(function($httpProvider) {
	    
	    var spinnerReqFunction    = function(data, headersGetter) {
		  if($('body #loading-backdrop').length <= 0) {
	         $('body').append(
				 '<div id="loading-backdrop">' +
	             '  <div id="loading">' +
	             '    Cargando <img src="../../imagene/buhocarga.gif" width="50px" />' +
	             '  </div>' +
	             '</div>');
	      }//fin de la condicion...
	      return data;
	    };
	    
	    var spinnerResFunction    = function(data, headersGetter) {
	       if($('body #loading-backdrop').length) {
	          $('body #loading-backdrop').remove();
	       }//end of condition...
	       return data;
		};
	    
	    $httpProvider.defaults.transformRequest.push(spinnerReqFunction);
	    $httpProvider.defaults.transformResponse.push(spinnerResFunction);
	})
	
	.service('smartOlwServices', function($http){
		$http.defaults.useXDomain = true;
		var headers = {
				'Access-Control-Allow-Origin' : '*',
				'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',
				'Access-Control-Allow-Headers' : 'Content-Type, Accept, X-Requested-With',
				'Content-Type': 'application/json',
				'Accept': 'application/json'
			};
		
		this.sendGet = function(urlEndPoint,success,error){
			
			$http({
			  url:urlEndPoint,
			  method: 'GET',
			  headers:headers
		    }).then(
		    		function(response){
		    			success(response);
		    		},
		    		function(response){
		    			error(response);
		    		}
		    );
			
		}
		
		this.sendPost = function(json,urlEndPoint,success,error){
				
				$http({
				  url:urlEndPoint,
				  method: 'POST',
				  data: json,
				  headers:headers
			    }).then(
			    		function(response){
			    			success(response);
			    		},
			    		function(response){
			    			error(response);
			    		}
			    );
				
			}
		
			this.sendPut = function(json,urlEndPoint,success,error){
					
					$http({
					  url:urlEndPoint,
					  method: 'PUT',
					  headers: headers,
				  data: json
			    }).then(
			    		function(response){
			    			success(response);
			    		},
			    		function(response){
			    			error(response);
			    		}
			    );
		}
		
			
		this.validateInfo 	= function() {
			var data		= arguments[0];
			var isValidData	= arguments[1];
			var whenIsTrue	= arguments[2];
			var whenisFalse = (arguments.length > 2)?arguments[3]:null;
			
			if(isValidData(data)) {
			   whenIsTrue(angular.copy(data));
			} 
			else if(whenisFalse != null) {
			   whenisFalse(angular.copy(data));
			}
		};
		
		this.focus			= function() {
			var idField		= arguments[0];
			var domField	= $('#'+idField);
			 if(domField != null) {
				domField.focus(); 
			 }
		};
});